﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class CondicionesDA
    {
        private CONDICIONESTableAdapter Adapter = new CONDICIONESTableAdapter();
        private DataSet.CONDICIONESDataTable entities;
        private DataSet.CONDICIONESRow entity;

        public decimal Insert(CONDICIONES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.CONDICIONESDataTable();

                    entity = entities.NewCONDICIONESRow();

                    entity.ID = value.ID;
                    entity.CONDICION = value.CONDICION.Trim();

                    entities.AddCONDICIONESRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(CONDICIONES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();

                    entity.CONDICION = value.CONDICION.Trim();

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public CONDICIONES GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    CONDICIONES condicion;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        condicion = new CONDICIONES
                            {
                                ID = entity.ID,
                                CONDICION = entity.CONDICION,
                            };
                    }
                    else
                        condicion = null;

                    return condicion;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<CONDICIONES> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<CONDICIONES> lst = new List<CONDICIONES>();
                    foreach (DataSet.CONDICIONESRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new CONDICIONES
                        {
                            ID = rw.ID,
                            CONDICION = rw.CONDICION,
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }       
    }
}
